package com.ems.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ems.config.MyToken;
import com.ems.exception.Result;
import com.ems.exception.ResultCode;
import com.ems.service.LoginService;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Override
    public Object login(String username,String password,Integer position) {

        System.out.println("请求来了");
//        String username = jsonObject.getString("username");
//        String password = jsonObject.getString("password");
//        Integer position = jsonObject.getInteger("position");

        Subject subject = SecurityUtils.getSubject();
        MyToken token = new MyToken(username, password, position);

        try{
            subject.login(token);
            return Result.ok();
        }
        catch (UnknownAccountException e){
            //用户名不存在
            return Result.state(ResultCode.NULLUSERNAME);
        }
        catch (IncorrectCredentialsException e){
            //密码错误
            return Result.state(ResultCode.ERROR_PWD);
        }
    }
}
